// ----------------------------------
// RSDK Project: Sonic 2
// Script Description: Bee Object
// Script Author: Christian Whitehead/Simon Thomley
// Unpacked by Rubberduckycooly's script unpacker
// ----------------------------------

// ========================
// Aliases
// ========================

private alias object.value0 : object.timer
private alias object.value1 : object.startPos.x
private alias object.value2 : object.hasShot
private alias object.value3 : object.shootTimer

// States
private alias 0 : BEE_FLYING
private alias 1 : BEE_STOPPED
private alias 2 : BEE_SHOOTING
private alias 3 : BEE_INVISIBLE

// Animations
private alias 0 : BEE_ANI_FLYING
private alias 1 : BEE_ANI_STOPPED
private alias 2 : BEE_ANI_SHOOTING
private alias 3 : BEE_ANI_INVISIBLE

// Player Aliases
private alias object.value40 : player.hitboxLeft
private alias object.value38 : player.hitboxTop
private alias object.value41 : player.hitboxRight
private alias object.value39 : player.hitboxBottom


// ========================
// Function Declarations
// ========================

reserve function Bee_DebugDraw
reserve function Bee_DebugSpawn


// ========================
// Function Definitions
// ========================

private function Bee_DebugDraw
	DrawSpriteFX(0, FX_FLIP, object.xpos, object.ypos)
end function


private function Bee_DebugSpawn
	CreateTempObject(TypeName[Bee], 0, object.xpos, object.ypos)
	GetBit(temp0, object.direction, 0)
	if temp0 == FACING_RIGHT
		object[tempObjectPos].direction = FLIP_NONE
		object[tempObjectPos].xvel = -0x10000
	else
		object[tempObjectPos].direction = FLIP_X
		object[tempObjectPos].xvel = 0x10000
	end if
end function


// ========================
// Events
// ========================

event ObjectUpdate
	switch object.state
	case BEE_FLYING
		object.priority = PRIORITY_ACTIVE
		if object.timer < 256
			object.timer++
			object.xpos += object.xvel
		else
			object.timer = 0
			object.state = BEE_STOPPED
			object.animation = BEE_ANI_STOPPED
			object.direction ^= FLIP_X
			FlipSign(object.xvel)
		end if
		break

	case BEE_STOPPED
		if object.shootTimer < 30
			object.shootTimer++
		else
			object.shootTimer = 0
			object.state = BEE_FLYING
			object.animation = BEE_ANI_FLYING
			object.hasShot = false
		end if
		break

	case BEE_SHOOTING
		if object.shootTimer < 50
			object.shootTimer++
			if object.shootTimer == 30
				CreateTempObject(TypeName[Bee Shot], 0, object.xpos, object.ypos)
				object[tempObjectPos].direction = object.direction
				if object.direction == FLIP_NONE
					object[tempObjectPos].xpos += 0xD0000
					object[tempObjectPos].xvel = -0x18000
				else
					object[tempObjectPos].xpos -= 0xD0000
					object[tempObjectPos].xvel = 0x18000
				end if
				object[tempObjectPos].ypos += 0x180000
				object[tempObjectPos].yvel = 0x18000
			end if
		else
			object.shootTimer = 0
			object.state = BEE_FLYING
			object.animation = BEE_ANI_FLYING
			object.hasShot = true
		end if
		break

	case BEE_INVISIBLE
		object.animation = BEE_ANI_INVISIBLE
		break
		
	end switch

	if object.outOfBounds == true
		temp0 = object.xpos
		object.xpos = object.startPos.x
		if object.shootTimer == 1
			object.state = BEE_INVISIBLE
		end if

		if object.outOfBounds == true
			object.xpos = object.startPos.x

			GetBit(temp0, object.propertyValue, 0)
			if temp0 == 0
				object.direction = FLIP_NONE
				object.xvel = -0x10000
			else
				object.direction = FLIP_X
				object.xvel = 0x10000
			end if

			object.timer = 0
			object.animation = BEE_ANI_FLYING
			object.priority = PRIORITY_BOUNDS
			object.state = BEE_FLYING
		else
			object.xpos = temp0
		end if
	end if

	foreach (GROUP_PLAYERS, currentPlayer, ACTIVE_ENTITIES)
		if object.state == BEE_FLYING
			if object.hasShot == false
				if object.direction == FLIP_NONE
					BoxCollisionTest(C_TOUCH, object.entityPos, -28, -256, -30, 256, currentPlayer, C_BOX, C_BOX, C_BOX, C_BOX)
				else
					BoxCollisionTest(C_TOUCH, object.entityPos, 28, -256, 30, 256, currentPlayer, C_BOX, C_BOX, C_BOX, C_BOX)
				end if

				if checkResult == true
					object.state = BEE_SHOOTING
					object.animation = BEE_ANI_SHOOTING
				end if
			end if
		end if

		if object.state < BEE_INVISIBLE
			BoxCollisionTest(C_TOUCH, object.entityPos, -24, -12, 24, 12, currentPlayer, player[currentPlayer].hitboxLeft, player[currentPlayer].hitboxTop, player[currentPlayer].hitboxRight, player[currentPlayer].hitboxBottom)
			if checkResult == true
				CallFunction(Player_BadnikBreak)
			end if
		end if
	next

	object.frame = object.animationTimer
	object.frame /= 3
	object.frame += 3
	object.animationTimer++
	if object.animationTimer >= 6
		object.animationTimer = 0
	end if
end event


event ObjectDraw
	switch object.animation
	case BEE_ANI_FLYING
		DrawSpriteFX(0, FX_FLIP, object.xpos, object.ypos)
		DrawSpriteFX(object.frame, FX_FLIP, object.xpos, object.ypos)
		break

	case BEE_ANI_STOPPED
		DrawSpriteFX(0, FX_FLIP, object.xpos, object.ypos)
		break

	case BEE_ANI_SHOOTING
		DrawSpriteFX(1, FX_FLIP, object.xpos, object.ypos)

		if object.shootTimer > 20
			if object.shootTimer < 30
				DrawSpriteFX(2, FX_FLIP, object.xpos, object.ypos)
			end if
		end if
		break

	case BEE_ANI_INVISIBLE
		break
		
	end switch
end event


event ObjectStartup
	CheckCurrentStageFolder("Zone01")
	if checkResult == true
		LoadSpriteSheet("EHZ/Objects.gif")
		SpriteFrame(-24, -8, 48, 18, 78, 1)
		SpriteFrame(-24, -8, 42, 31, 146, 224)
		SpriteFrame(1, 6, 17, 17, 50, 1)
		SpriteFrame(5, -8, 6, 5, 19, 50)
		SpriteFrame(5, -8, 7, 5, 19, 56)
	else
		LoadSpriteSheet("OWZ/Objects.gif")
		
		// This is strange - they *partially* adjusted some of the frames for MBZ?
		// The ones shared with the Buzzer (which is used in MBZ) got updated, while the ones unique to the Bee were neglected
		// How strange...
		
		SpriteFrame(-24, -8, 71, 27, 78, 1)
		SpriteFrame(-24, -8, 48, 39, 140, 216)
		SpriteFrame(1, 6, 17, 17, 50, 1)
		SpriteFrame(5, -8, 6, 5, 19, 50)
		SpriteFrame(5, -8, 7, 5, 19, 56)
	end if

	foreach (TypeName[Bee], arrayPos0, ALL_ENTITIES)
		object[arrayPos0].startPos.x = object[arrayPos0].xpos

		GetBit(temp0, object[arrayPos0].propertyValue, 0)
		if temp0 == 0
			object[arrayPos0].direction = FLIP_NONE
			object[arrayPos0].xvel = -0x10000
		else
			object[arrayPos0].direction = FLIP_X
			object[arrayPos0].xvel = 0x10000
		end if
	next

	SetTableValue(TypeName[Bee], DebugMode_ObjCount, DebugMode_TypesTable)
	SetTableValue(Bee_DebugDraw, DebugMode_ObjCount, DebugMode_DrawTable)
	SetTableValue(Bee_DebugSpawn, DebugMode_ObjCount, DebugMode_SpawnTable)
	DebugMode_ObjCount++
end event


// ========================
// Editor Events
// ========================

event RSDKEdit
	if editor.returnVariable == true
		switch editor.variableID
		case EDIT_VAR_PROPVAL // property value
			checkResult = object.propertyValue
			break
			
		case 0 // direction
			checkResult = object.propertyValue
			break
			
		end switch
	else
		switch editor.variableID
		case EDIT_VAR_PROPVAL // property value
			object.propertyValue = editor.variableValue
			break
			
		case 0 // direction
			object.propertyValue = editor.variableValue
			break
			
		end switch
	end if
end event


event RSDKDraw
	GetBit(object.direction, object.propertyValue, 0)
	DrawSpriteFX(0, FX_FLIP, object.xpos, object.ypos)
	DrawSpriteFX(3, FX_FLIP, object.xpos, object.ypos)
end event


event RSDKLoad
	CheckCurrentStageFolder("Zone01")
	if checkResult == true
		LoadSpriteSheet("EHZ/Objects.gif")
		SpriteFrame(-24, -8, 48, 18, 78, 1)
		SpriteFrame(-24, -8, 42, 31, 146, 224)
		SpriteFrame(1, 6, 17, 17, 50, 1)
		SpriteFrame(5, -8, 6, 5, 19, 50)
		SpriteFrame(5, -8, 7, 5, 19, 56)
	else
		LoadSpriteSheet("MBZ/Objects.gif")
		SpriteFrame(-24, -8, 48, 18, 78, 1)
		SpriteFrame(-24, -8, 42, 31, 146, 224)
		SpriteFrame(1, 6, 17, 17, 50, 1)
		SpriteFrame(5, -8, 6, 5, 137, 331)
		SpriteFrame(5, -8, 7, 5, 137, 337)
	end if

	AddEditorVariable("direction")
	SetActiveVariable("direction")
	AddEnumVariable("Left", 0)
	AddEnumVariable("Right", 1)
end event
